﻿using System.Data;
using JKF.BusinessLayer.BLL;
using JKF.Entities;
using JKF.Entities.Model;
using JKF.Substructure;
using SFWS.XHandler.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JKF.HandlerLayer.Feedback
{
    public class FeedbackHandler : AHttpHandler
    {
        public override void ProcessRequest()
        {
            try
            {
                this.HandleRequest(Utils.GetActionName);
            }
            catch (Exception ex)
            {
                //将错误抛到客户端(以Json格式)
                Utils.ToClient(true, ex.Message);
                AppLogger.Log(ex.Message, ex);  //写日志
            }
        }

        public void GetFeedBacks()
        {
            bllFEEDBACK bllFeedBack = new bllFEEDBACK();
            var obj = new PagerList();

            int pageCount = 0;
            int rowCount = 0;

            //type=0 在线咨询 1公告
            obj.Entity = from a in bllFeedBack.GetFeedBacks(P("title"), P("status"), P("type"), P("PageIndex").ToInt32(), P("PageSize").ToInt32(), ref pageCount, ref rowCount).AsEnumerable()
                         select new
                         {
                             FEEDBACK_KEY = a.Field<Guid>("FEEDBACK_KEY").ToString(),
                             TITLE = a.Field<string>("TITLE"),
                             FEEDBACKER = a.Field<string>("FEEDBACKER"),
                             FEEDBACK_DATE = a.Field<string>("FEEDBACK_DATE"),
                             IS_REPLIED = a.Field<string>("IS_REPLIED").ToString(),
                             IS_REPLIED_CN = a.Field<string>("IS_REPLIED").ToString()=="0"?"未回复":"已回复",
                             SHOP_CODE = a.Field<string>("SHOP_CODE").ToString(),
                             FILE_PATH =GetFileName(a.Field<string>("FILE_PATH").ToString()),
                         };

            obj.RowCount = rowCount;
            obj.PageCount = pageCount;
            ResponseJsonObject = obj;
        }

        public static string GetFileName(String path)
        {
            if (string.IsNullOrEmpty(path))
                return "";

            if (path.Contains("\\"))
            {
                string[] arr = path.Split('\\');
                return "/UploadFiles/"+arr[arr.Length - 1];
            }
            else
            {
                string[] arr = path.Split('/');
                return "/UploadFiles/"+arr[arr.Length - 1];
            }
        }


        /// <summary>
        /// 取得最近6条反馈记录在首页显示
        /// </summary>
        public void GetLatestFeedback()
        {
            bllFEEDBACK bllFeedback = new bllFEEDBACK();
            
            Response.Write(JsonSerializer.Serialize(bllFeedback.GetLatestFeedback()));
        }

        public void DeleteFeedback()
        {
            try
            {
                var feedbackKey = P("feedbackKey");
                bllFEEDBACK bllFeedback = new bllFEEDBACK();
                bllFeedback.DeleteFeedback(feedbackKey);
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = feedbackKey }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

         public class FEEDBACK
        {
            public string FEEDBACK_KEY;
            public string TITLE;
            public string FEEDBACKER;
            public string FEEDBACK_DATE;
            public string CONTENT;
            public string FILE_PATH;
            public string TYPE;
        }

        public FEEDBACKinfo  BuildFeedbackInfoByJson(FEEDBACK feedbackInfo)
        {
            var entity = new FEEDBACKinfo();

            if (!string.IsNullOrEmpty(feedbackInfo.FEEDBACK_KEY))
                entity.FEEDBACK_KEY =new Guid(feedbackInfo.FEEDBACK_KEY);

            entity.FILE_PATH = feedbackInfo.FILE_PATH;
            entity.TITLE = feedbackInfo.TITLE;
            entity.FEEDBACKER = feedbackInfo.FEEDBACKER;
            entity.FEEDBACK_DATE =DateTime.Parse(feedbackInfo.FEEDBACK_DATE);
            entity.CONTENT =feedbackInfo.CONTENT;
            entity.TYPE = feedbackInfo.TYPE;
            return entity;
        }

        public void EditOrInsertFeedback()
        {
            try
            {
                bllFEEDBACK bllFeedback = new bllFEEDBACK();
                var strScr = P("feedbackObj");
                var feedBackInfo = BuildFeedbackInfoByJson(JsonSerializer.Deserialize<FEEDBACK>(strScr));
                var tid = feedBackInfo.FEEDBACK_KEY.ToString();
                var pk = bllFeedback.EditOrInsertFeedback(feedBackInfo);
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = tid.IsGuidB() ? "编辑成功." : "添加成功.", Data = pk }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        public void ReplyFeedback()
        {
            try
            {
                bllFEEDBACK bllFeedback = new bllFEEDBACK();
                var strScr = P("feedbackObj");
                var feedbackKey = P("FEEDBACK_KEY");
                var repliedContent = P("REPLIED_CONTENT");

                bllFeedback.ReplyFeedback(feedbackKey, repliedContent);
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = "操作成功.", Data = feedbackKey }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        public void GetFeedbackByKey()
        {
            bllFEEDBACK bllFeedback = new bllFEEDBACK();
            try
            {
                var scid = P("feedbackKey");
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = bllFeedback.GetFeedbackInfo(scid) }, Utils.GetDateFormat));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }
    }
}
